use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_post table
        manager
            .create_table(
                Table::create()
                    .table(SysPost::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysPost::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysPost::PostCode).string())
                    .col(ColumnDef::new(SysPost::PostName).string().not_null())
                    .col(ColumnDef::new(SysPost::Sort).integer())
                    .col(ColumnDef::new(SysPost::Status).integer().default(1))
                    .col(ColumnDef::new(SysPost::TenantId).big_integer())
                    .col(ColumnDef::new(SysPost::Creator).big_integer())
                    .col(ColumnDef::new(SysPost::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysPost::Updater).big_integer())
                    .col(ColumnDef::new(SysPost::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysPost::Version).integer().default(0))
                    .col(ColumnDef::new(SysPost::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // Create sys_user_post table
        manager
            .create_table(
                Table::create()
                    .table(SysUserPost::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysUserPost::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysUserPost::UserId).big_integer().not_null())
                    .col(ColumnDef::new(SysUserPost::PostId).big_integer().not_null())
                    .col(ColumnDef::new(SysUserPost::Creator).big_integer())
                    .col(ColumnDef::new(SysUserPost::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysUserPost::Updater).big_integer())
                    .col(ColumnDef::new(SysUserPost::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysUserPost::Version).integer().default(0))
                    .col(ColumnDef::new(SysUserPost::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // Create sys_role_data_scope table
        manager
            .create_table(
                Table::create()
                    .table(SysRoleDataScope::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysRoleDataScope::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysRoleDataScope::RoleId).big_integer().not_null())
                    .col(ColumnDef::new(SysRoleDataScope::OrgId).big_integer().not_null())
                    .col(ColumnDef::new(SysRoleDataScope::Creator).big_integer())
                    .col(ColumnDef::new(SysRoleDataScope::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysRoleDataScope::Updater).big_integer())
                    .col(ColumnDef::new(SysRoleDataScope::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysRoleDataScope::Version).integer().default(0))
                    .col(ColumnDef::new(SysRoleDataScope::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysRoleDataScope::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysUserPost::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysPost::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysPost {
    Table,
    Id,
    PostCode,
    PostName,
    Sort,
    Status,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}

#[derive(DeriveIden)]
enum SysUserPost {
    Table,
    Id,
    UserId,
    PostId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}

#[derive(DeriveIden)]
enum SysRoleDataScope {
    Table,
    Id,
    RoleId,
    OrgId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}